package com.wisdudu.blank.ui.login;

import android.Manifest;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wisdudu.blank.R;
import com.wisdudu.blank.databinding.FragmentLoginBinding;
import com.wisdudu.frame.base.BaseFragment;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by qyf on 2016/8/11.
 */

public class LoginFragment extends BaseFragment implements EasyPermissions.PermissionCallbacks {
    private static final int REQUEST_CODE_READPHONESTATE_PERMISSIONS = 101;
    private LoginVM mLoginVM;

    @Override
    protected View initBinding(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        FragmentLoginBinding mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_login, container, false);
        mLoginVM = new LoginVM(this, mBinding);
        mBinding.setViewModel(mLoginVM);
        return mBinding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        requestCodeQrcodePermissions();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLoginVM.onDestroy();
    }

    @AfterPermissionGranted(REQUEST_CODE_READPHONESTATE_PERMISSIONS)
    private void requestCodeQrcodePermissions() {
        String[] perms = {Manifest.permission.READ_PHONE_STATE};
        if (!EasyPermissions.hasPermissions(mActivity, perms)) {
            EasyPermissions.requestPermissions(this, "扫描二维码需要打开相机和散光灯的权限", REQUEST_CODE_READPHONESTATE_PERMISSIONS, perms);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

    }
}
